package com.rabbit.component;

import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {
  public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
  public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

@Autowired
  private RabbitTemplate rabbitTemplate;

@GetMapping("sendMsg/{message}")
  public void sendMsg(@PathVariable String message){
  log.info("当前时间: {},发送一条消息给两个TTL队列:{}",new Date(),message);
  rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列"+message);
  rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列"+message);


}

@GetMapping("sendDelayMsg/{message}/{delayTime}")
  public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime){
  rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME,DELAYED_ROUTING_KEY,message,correlationData ->{
    correlationData.getMessageProperties().setDelay(delayTime);
  return correlationData;
  });
  log.info(" 当前时间：{},发送一条延迟{}毫秒的信息给队列 delayed.queue:{}",new Date(),delayTime,message);
}
}
